package com.qiyu.mq.springboot.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 保证消费端能够顺序性消费
 *
 * @Author idea
 * @Date created in 11:56 上午 2020/8/8
 */
@Slf4j
@Component
public class MessageListenerHandler implements MessageListenerConcurrently {

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        if (CollectionUtils.isEmpty(list)) {
            log.info("receive empty msg");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        //对于消息的扩展内容，可以从中获取到更多信息
        MessageExt messageExt = list.get(0);
        handleMsg(messageExt);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    public void handleMsg(MessageExt messageExt) {
        log.info("msg is : {}", messageExt);
    }
}
